package main

import (
	"gomicro/webGoMicro/webClient/controller"
	_ "gomicro/webGoMicro/webClient/controller"
	"gomicro/webGoMicro/webClient/wrappers"

	//"gowork/webGoMicro/webMessage"
	"log"

	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"github.com/micro/go-micro/v2"

	// "github.com/micro/go-micro/v2/client"
	"github.com/micro/go-micro/v2/client/selector"

	"github.com/micro/go-micro/v2/web"
	"github.com/micro/go-plugins/registry/etcdv3/v2"
	//grpc "github.com/micro/go-grpc"
	"gomicro/webGoMicro/webClient/util"
	"gomicro/webGoMicro/webMessage"
)

func main() {

	// grpcService := grpc.NewService(
	// 	micro.Name("go.micro.api.student"),
	// )

	// grpcService.Init()
	
	//util.EtcdRegistry = etcdv3.NewRegistry()
	etcdRegistry := etcdv3.NewRegistry()
	
	//selector.SetStrategy(selector.Random)
	microService := micro.NewService(
		micro.Selector(selector.NewSelector(selector.Registry(etcdRegistry),
			selector.SetStrategy(selector.RoundRobin),)),
		micro.Registry(etcdRegistry),
		//micro.Registry(util.EtcdRegistry),
		micro.WrapClient(wrappers.NewLogWrapper),
		micro.WrapClient(wrappers.NewClientWrapper()),
	)

	util.Cli = webMessage.NewStudentService("student_micro_service", microService.Client())

	service := web.NewService(
		web.Name("go.micro.api.student"),
		web.Registry(etcdRegistry),
	)

	microService.Init()

	service.Init()
	
	// getService , sErr := util.EtcdRegistry.GetService("student_micro_service")
	// if sErr != nil{
	// 	log.Fatal(sErr)
	// }
	// netx := selector.RoundRobin(getService)
	// note, err := netx()
	// if err != nil {
	// 	log.Fatal(note)
	// }

	// controller.Cli = webMessage.NewStudentService(note.Address, client.DefaultClient)

	app := iris.New()
	mvc.New(app).Handle(new(controller.StudentMicroController))
	app.Run(iris.Addr(":0002"))

	service.Handle("/", app)

	if err := service.Run(); err != nil {
		log.Fatal(err)
	}

}

/*
自定义负载均衡：创建结构体，实现Selector接口，初始化 //micro.Selector(&结构体实例),
*/